﻿using InTheHand.Net.Sockets;
using System;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Windows;

namespace BluetoothDemoApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BluetoothClient bluetoothClient = new BluetoothClient();
        private NetworkStream networkStream;
        private BluetoothDeviceInfo selectedDevice;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnSearch.IsEnabled = false;
                BluetoothClient client = new BluetoothClient();
                var devices = client.DiscoverDevices();

                lbDevices.ItemsSource = devices;
                btnSearch.IsEnabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"搜索设备失败: {ex.Message}");
            }
        }

        private void BtnConnect_Click(object sender, RoutedEventArgs e)
        {
            if (lbDevices.SelectedItem is BluetoothDeviceInfo deviceInfo)
            {
                try
                {
                    selectedDevice = deviceInfo;

                    var address = deviceInfo.DeviceAddress;

                    bluetoothClient.Connect(address, deviceInfo.InstalledServices.FirstOrDefault());
                    MessageBox.Show("连接成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"连接失败: {ex.Message}");
                }
            }
            else
            {
                MessageBox.Show("请选择一个设备进行连接");
            }
        }

        private void BtnSend_Click(object sender, RoutedEventArgs e)
        {
            byte[] bytes = null;
            using (FileStream fileStream = new FileStream("e:\\screening_image_ai_content.sql", FileMode.Open, FileAccess.Read))
            {
                using MemoryStream memoryStream = new MemoryStream();
                byte[] buffer = new byte[1024];
                int dataSize = 0;
                while ((dataSize = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    memoryStream.Write(buffer, 0, dataSize);
                }
                bytes = memoryStream.ToArray();
                memoryStream.Close();
                fileStream.Close();
            }
            if (bluetoothClient != null && selectedDevice != null)
            {
                try
                {
                    if (!selectedDevice.Connected)
                    {
                        bluetoothClient.Connect(selectedDevice.DeviceAddress, selectedDevice.InstalledServices.FirstOrDefault());
                    }
                    // 通过流发送数据
                    if (networkStream == null)
                        networkStream = bluetoothClient.GetStream();
                    {

                        string dataToSend = txtData.Text;
                        byte[] data = Encoding.ASCII.GetBytes(dataToSend + "\n");
                        networkStream.Write(bytes, 0, bytes.Length);
                        MessageBox.Show("数据发送成功");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"发送数据失败: {ex.Message}");
                }
            }
            else
            {
                MessageBox.Show("请先连接设备");
            }
        }
    }
}